﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.sim {
    import Engine.Classes.*;
    import Engine.Helpers.*;
    import Classes.*;
    import __AS3__.vec.*;
    import Modules.quest.Managers.*;
    import Classes.orders.*;

    public class FriendVisitManager implements IGameWorldStateObserver {

        protected var m_world:GameWorld = null;
        protected var m_helpers:Vector.<FriendVisitMgrHelper>;
        private var m_doDebug:Boolean = false;
        private var m_needToCheckQueue:Boolean = false;
        protected var m_delay:Number = 0;
        protected var m_numDbgBusiness:int;
        protected var m_numDbgResidence:int;
        protected var m_numDbgCrop:int;
        protected var m_numDbgWild:int;

        public function FriendVisitManager(_arg1:GameWorld){
            this.m_world = _arg1;
            this.m_helpers = new Vector.<FriendVisitMgrHelper>();
            _arg1.addObserver(this);
        }
        public function initialize():void{
        }
        public function cleanUp():void{
            var _local1:FriendVisitMgrHelper;
            for each (_local1 in this.m_helpers) {
                _local1.hidePick();
            };
        }
        public function onGameLoaded(_arg1:Object):void{
            if (Global.isVisiting()){
            };
            if (Global.isVisiting()){
            };
            if (!Global.isVisiting()){
                this.m_needToCheckQueue = true;
            };
        }
        public function onResourceChange(_arg1:MapResource, _arg2:Vector3, _arg3:Vector3):void{
        }
        public function helperFinished(_arg1:FriendVisitMgrHelper):void{
            var _local2:int = this.m_helpers.indexOf(_arg1);
            if (_local2 >= 0){
                this.m_helpers.splice(_local2, 1);
            };
        }
        public function closeAllPicks(_arg1:FriendVisitMgrHelper):void{
            var _local2:int = this.m_helpers.indexOf(_arg1);
            var _local3:int;
            while (_local3 < this.m_helpers.length) {
                if (_local3 != _local2){
                    this.m_helpers[_local3].closePick();
                };
                _local3++;
            };
        }
        private function storesNHomes(_arg1:WorldObject):Boolean{
            var _local2:Residence;
            if ((_arg1 is Business)){
                if (this.m_numDbgBusiness < 2){
                    this.m_numDbgBusiness++;
                    return (true);
                };
                return (false);
            };
            if ((_arg1 is Residence)){
                _local2 = (_arg1 as Residence);
                if (_local2.getGrowPercentage() == 100){
                    if (this.m_numDbgResidence < 3){
                        this.m_numDbgResidence++;
                        return (true);
                    };
                };
                return (false);
            };
            return (false);
        }
        private function plotsAndWild(_arg1:WorldObject):Boolean{
            var _local2:Plot;
            if ((_arg1 is Plot)){
                _local2 = (_arg1 as Plot);
                if ((((_local2.getGrowPercentage() > 0)) && ((this.m_numDbgCrop < 2)))){
                    this.m_numDbgCrop++;
                    return (true);
                };
            };
            if ((_arg1 is Wilderness)){
                if (this.m_numDbgWild < 2){
                    this.m_numDbgWild++;
                    return (true);
                };
            };
            return (false);
        }
        public function setupDebug():void{
            var _local3:GameObject;
            var _local4:Player;
            var _local5:VisitorHelpOrder;
            var _local6:FriendVisitMgrHelper;
            if (this.m_helpers.length > 0){
                return;
            };
            this.m_numDbgBusiness = 0;
            this.m_numDbgResidence = 0;
            this.m_numDbgCrop = 0;
            this.m_numDbgWild = 0;
            var _local1:Array = Global.world.getObjectsByPredicate(this.storesNHomes);
            _local1 = _local1.concat(Global.world.getObjectsByPredicate(this.plotsAndWild));
            if (_local1.length == 0){
                return;
            };
            var _local2:Array = new Array();
            for each (_local3 in _local1) {
                _local2.push(_local3.getId());
            };
            _local4 = Global.player.getFakeFriend();
            _local5 = new VisitorHelpOrder(Global.player.uid, _local4.uid, _local2, OrderStatus.RECEIVED);
            _local6 = new FriendVisitMgrHelper(_local4, _local5, this);
            this.m_helpers.push(_local6);
        }
        protected function checkQueue():void{
            var _local1:Array;
            var _local2:VisitorHelpOrder;
            var _local3:FriendVisitMgrHelper;
            var _local4:Player;
            if (((!(Global.guide.isActive())) && (Global.questManager.isUXUnlocked(GameQuestManager.QUEST_UX_VISITS_UNLOCKED)))){
                this.m_needToCheckQueue = false;
                this.m_helpers = new Vector.<FriendVisitMgrHelper>();
                _local1 = Global.world.orderMgr.getOrders(OrderType.VISITOR_HELP, OrderStatus.RECEIVED);
                for each (_local2 in _local1) {
                    if (_local2.getStatus() == VisitorHelpOrder.UNCLAIMED){
                        _local4 = Global.player.findFriendById(_local2.getSenderID());
                        this.m_helpers.push(new FriendVisitMgrHelper(_local4, _local2, this));
                    };
                };
                for each (_local3 in this.m_helpers) {
                    _local3.showPick();
                };
            };
        }
        public function setNeedToCheckQueue():void{
            this.m_needToCheckQueue = true;
        }
        public function update(_arg1:Number):void{
            var _local2:FriendVisitMgrHelper;
            if (this.m_needToCheckQueue){
                this.checkQueue();
            };
            if (Global.ui.mouseEnabled == false){
                return;
            };
            if (this.m_delay > 0){
                this.m_delay = (this.m_delay - _arg1);
                return;
            };
            if (this.m_doDebug){
                this.setupDebug();
                this.m_doDebug = false;
            };
            for each (_local2 in this.m_helpers) {
                _local2.update(_arg1);
            };
        }

    }
}//package Classes.sim 
